<template>
  <view class="body">
    <!-- 顶部弹出栏 -->
    <view class="uni-pop-top">
      <u-popup border-radius="10" v-model="showSearchPop" mode="top" :mask="true" :closeable="false">
        <view style="margin-top: 10px;">
          <!--将搜索分开写，避免取消重置时字段之间互相影响-->
          <view v-if="current==0">
            <view class="pop-item">
              <text class="pop-item-text">物料编码</text>
              <u-input v-model="categoryCodeSingle" type="text" :border="false" placeholder="请输入"/>
            </view>
            <view class="pop-item">
              <text class="pop-item-text">物料名称</text>
              <u-input v-model="categoryNameSingle" type="text" :border="false" placeholder="请输入"/>
            </view>
            <view class="pop-item" v-if="type=='2'">
              <text class="pop-item-text">站址编码</text>
              <u-input v-model="siteIdSingle" type="text" :border="false" placeholder="请输入"/>
            </view>
            <view class="pop-item" v-if="type=='3'">
              <text class="pop-item-text">单项工程编码</text>
              <u-input v-model="prjCode" type="text" :border="false" placeholder="请输入"/>
            </view>
          </view>
          <view v-if="current==1">
            <view class="pop-item">
              <text class="pop-item-text">申领单号</text>
              <u-input v-model="recipientApplyId" type="text" :border="false" placeholder="请输入"/>
            </view>
            <view class="pop-item" v-if="type=='2'">
              <text class="pop-item-text">站址编码</text>
              <u-input v-model="siteIdBatch" type="text" :border="false" placeholder="请输入"/>
            </view>
          </view>
          <view v-if="current==2">
            <view class="pop-item">
              <text class="pop-item-text">物料编码</text>
              <u-input v-model="categoryCodeWhole" type="text" :border="false" placeholder="请输入"/>
            </view>
            <view class="pop-item">
              <text class="pop-item-text">物料名称</text>
              <u-input v-model="categoryNameWhole" type="text" :border="false" placeholder="请输入"/>
            </view>
            <view class="pop-item" v-if="type=='2'">
              <text class="pop-item-text">站址编码</text>
              <u-input v-model="siteIdWhole" type="text" :border="false" placeholder="请输入"/>
            </view>
          </view>
          <view class="pop-btn">
            <view @click.stop="hideSearchPop" class="pop-btn-cancel">取消</view>
            <view v-debounce="confirmSearchPop" class="pop-btn-confirm">确定</view>
          </view>
        </view>
      </u-popup>
    </view>
    <!-- 滑块 -->
    <view class="huakuai-tabs">
      <u-tabs ref="uTabs" :list="list" :current="current" @change="tabsChange" :is-scroll="false"
              swiperWidth="750" bar-width="250" active-color="#EB4B4B" inactive-color="#BEBDBD" font-size="32">
      </u-tabs>
    </view>
    <applyForList :flagLister="flagLister"  :type="type" :prjInfo="prjInfo" :showAdjustButton="showAdjustButton"
                  :isNewbusinessCode="isNewbusinessCode" @changeList="changeList" v-show="current==0" :manageMode="manageMode"></applyForList>
    <applicationFormList :baseLister="baseLister" :prjInfo="prjInfo" :type="type" :prjId="prjId" @changeList="changeList"
                         v-show="current==1"></applicationFormList>
    <OutOrderList :chuKuParams="chuKuParams" :chuKuOrderList="chukulister" :prjInfo="prjInfo" :type="type" @changeList="changeList"
                  v-show="current==2"/>
  </view>
</template>

<script>
import talkingdata from '../../common/talkingdata.js'
import OutOrderList from './outOrderList.vue'
import NumberBox from "@/component/numberBox/u-number-box.vue";
import ApplyForList from "@/inspur/materialRequisition/applyForList.vue";
import ApplicationFormList from "@/inspur/materialRequisition/applicationFormList.vue";

export default {
  components: {
    ApplyForList,
    ApplicationFormList,
    NumberBox,
    OutOrderList
  },
  onPullDownRefresh() {
    this.changeList();
    setTimeout(function () {
      uni.stopPullDownRefresh()
    }, 1000)
  },
  data() {
    return {
        showAdjustButton:false,
        taskDetailId:'',
      type: '2', //单站1 批量2 整体3
      current: 0,
      list: [{
        name: "物资申领",
      },
        {
          name: "物资申领单",
        },
        {
          name: "出库单/退库",
        }
      ],
      materialList: [],
      flagList: [],
      flagLister: [],
      baseLister: [],
      chukulister: [], // 出库单/退库
      chuKuParams: {}, // 出库参数
      prjId: '',
      wzmc: '',
      wzmcer: '',
      wzmcera: '',
      sldh: '',
      ckdh: '',
      showSearchPop: false, //导航栏漏斗显隐
      categoryNameSingle: '', //搜索框物料名称
      categoryNameWhole: '', //搜索框物料名称
      categoryCodeSingle: '', //搜索框物料编码
      categoryCodeWhole: '', //搜索框物料编码
      prjCode: '', //搜索框单项工程编码
      recipientApplyId: '', //搜索框申领单号
      siteIdSingle: '', //搜索框站址编码
      siteIdBatch: '', //搜索框站址编码
      siteIdWhole: '', //搜索框站址编码
      isNewbusinessCode: '',//业务类型：0通信，1智联，2能源
      prjInfo: {}, //物资申领按钮传参需要
      manageMode:'',
    }
  },
  onLoad(option) {
    // console.log(option, "1.1.1开始渲染页面了")
    this.prjId = option.prjId;
    this.siteIdSingle = option.siteId;
    this.siteIdBatch = option.siteId;
    this.siteIdWhole = option.siteId;
    this.manageMode = option.manageMode;
    uni.setStorageSync("sysToken", option.sysToken)
    if (option.manageMode === '01') {
      this.type = '1'
    } else if (['02', '21'].includes(option.manageMode)) {
      this.type = '2'
    } else if (['03', '04', '31', '41'].includes(option.manageMode)) {
      this.type = '3'
    }else{
        console.log(this.type)
    }
  },

  onShow() {
    // 导航栏添加图标
    // const url = window.config.env[process.env.NODE_ENV].baseUrl
    // const url = process.uniEnv.commonPMS20
    const url = window.location.origin + window.location.pathname
    let menus = [{
      ico: url + "static/imges/screenIcon.png",
      callback: () => this.searchPop()
    }];
    imp.iWindow.setMenus(menus);
    console.log("2.2.2开始渲染页面了")
    this.baseLister = []
    this.materialList = []
    this.flagList = []
    this.chukulister = []
    this.changeList();
    this.getAdjustButton();
  },
  onReady() {
    console.log("执行onReady")
    //在页面挂载完成后，调用外部埋点，统计数据
    const script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = talkingdata.talkingKey;
    document.body.appendChild(script);
  },
  onHide() {
    console.log("执行onHide")
    imp.iWindow.setMenus([{
      ico: '',
      callback: () => {
          console.log('隐藏掉')
      }
    }]);
  },
  methods: {
      getAdjustButton(){
          this.$axios.get('/material/api/material/transfer/checkNodeByPrjId?prjId='+this.prjId,{},{hideMessage:true}).then((res)=>{
              this.showAdjustButton=true;
          })
      },
    //返回到之前的进入页面位置
    back() {
      if (!this.loading) {
        uni.navigateBack({
          delta: 1,
        });
      }
    },
    //导航栏漏斗搜索
    searchPop() {
      this.showSearchPop = true;
    },
    changeList() {
      this.getList();
      this.shenlingdanList();
      this.chukudanList();
    },
    hideSearchPop() {
      this.showSearchPop = false;
      if (this.current === 0) {
        this.categoryCodeSingle = '';
        this.categoryNameSingle = '';
        this.siteIdSingle = '';
        this.prjCode = '';
        this.getList();
      } else if (this.current === 1) {
        this.siteIdBatch = '';
        this.recipientApplyId = '';
        this.shenlingdanList();
      } else if (this.current === 2) {
        this.categoryCodeWhole = '';
        this.categoryNameWhole = '';
        this.siteIdWhole = '';
        this.chukudanList()
      }else{
          console.log('啥也不是')
      }
    },
    //过滤搜索
    confirmSearchPop() {
      this.showSearchPop = false;
      if (this.current === 0) {
        this.getList();
      } else if (this.current === 1) {
        this.shenlingdanList();
      } else if (this.current === 2) {
        this.chukudanList()
      }else{
          console.log('啥也不是')
      }
    },
    //滑块跳转
    tabsChange(index) {
      this.current = index;
    },
    //降序处理
    compare(property) {
      return function (a, b) {
        let value1 = a[property];
        let value2 = b[property];
        return value2 - value1;//升序,降序为value2 - value1
      }
    },
    // 物资申领数据
    getList() {
      this.materialList = [];
      this.flagList = [];
      this.flagLister = [];
      if (this.type !== '3') {
        let params = {
          prjId: this.prjId,
          categoryCode: this.categoryCodeSingle,
          categoryName: this.categoryNameSingle,
          siteId: this.siteIdSingle,
        }
        this.$axios.get("/material/api/receive/equip/queryAppReceiveDetails", params).then((res) => {
          this.prjInfo = res.data.prjInfo;
          this.isNewbusinessCode = res.data.prjInfo.isNewbusinessCode;
          let person = res.data.designData;
          person.sort(this.compare('noReceiveAmount'))
          this.materialList = person;
          //添加一个字段用来批量新增的时候给每一个对象加上receiveAmount
          this.materialList.map((item, index) => {
            this.flagList.push(Object.assign({}, item, {
              receiveAmount: 0,
              productApplyIndex: 0,
              selectedType: "请选择",
              selectedCode: "",
            }))
          });
          this.flagLister = this.flagList;
        })
      } else {
        let params = {
          prjId: this.prjId,
          categoryCode: this.categoryCodeSingle,
          categoryName: this.categoryNameSingle,
          prjCode: this.prjCode,
        }
        this.$axios.get("/material/api/receive/equip/queryAppReceiveDetails", params).then((res) => {
          this.prjInfo = res.data.prjInfo;
          this.isNewbusinessCode = res.data.prjInfo.isNewbusinessCode;
          let person = res.data.overallDesignInfo;
          person.sort(this.compare('totalNoReceiveAmount'))
          this.materialList = person;
          // 这是我写的
          let array = [];
          this.materialList.map((item, index) => {
            array = [];
            item.proList.map((v) => {
              array.push(Object.assign({}, v, {
                receiveAmount: 0,
                productApplyIndex: 0,
                selectedType: "请选择",
                selectedCode: "",
              }))
            })
            item.proList = array;
          });
          this.flagLister = this.materialList;
        })
      }
    },
    // 物资申领单数据
    shenlingdanList() {
      let params = {
        prjId: this.prjId,
        siteId: this.siteIdBatch,
        recipientApplyId: this.recipientApplyId,
      }
      this.$axios.get("/material/api/receive/equip/queryAppReceiveApply", params).then((res) => {
        this.baseLister = res.data || [];
      }).catch(() => {
        this.baseLister = [];
      })
    },
    // 出库单列表
    chukudanList() {
      console.log('传入参数' + this.prjId);
      console.log('传入参数1' + this.wzmc);
      console.log('传入参数2' + this.sldh);
      this.wzmcera = this.sldh;
      console.log('传入参数3' + this.ckdh);
      if (this.wzmcer) {
        console.log(Number(this.wzmcer))
        if ((Number(this.wzmcer))) {
          console.log("数字")
          this.wzmcera = this.wzmcer
          this.wzmc = ""
        } else {
          this.wzmcera = ""
          this.wzmc = this.wzmcer
        }
      }

      console.log('传入参数1' + this.wzmc);
      this.chuKuParams = {
        prjId: this.prjId,
        categoryCode: this.categoryCodeWhole,
        categoryName: this.categoryNameWhole,
        siteId: this.siteIdWhole,
        pageNum: 1,
        pageSize: 999
      }
      const urlP = this.type === '3' ? '/material/api/retreat/queryOsListWhole' : '/material/api/retreat/queryOsList'
      this.$axios.post(urlP, this.chuKuParams).then(res => {
        if (res.data?.osData?.length) {
          this.chukulister = res.data.osData
        } else {
          this.chukulister = []
          if (this.wzmcer || this.wzmc || this.sldh || this.ckdh) {
            uni.showModal({
              title: "提示",
              content: "没有数据",
              showCancel: false,
              confirmColor: '#eb4b4b',
            });
          }
        }
      })
    },

  },
}
</script>
<style>
@import "@/common/common-ui.css";
</style>
<style scoped lang="scss">
.body {
  height: 100%;
  width: 100%;
  background: transparent;

  .huakuai-tabs {
    // margin-top: 90rpx;
    padding: 0 2% 0 0;
    background-color: white;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 5;
    width: 100%;
  }
}

.uni-pop-top {
  .pop-item {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10rpx 30rpx;
    border-bottom: 2rpx solid #c0c4cc;

    .pop-item-text {
      font-size: 28rpx;
      color: #606266;
      width: 30%;
    }
  }

  .pop-btn {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 30rpx;

    .pop-btn-cancel {
      width: 25%;
      margin-right: 30rpx;
      height: 52rpx;
      font-size: 28rpx;
      color: rgb(235, 75, 75);
      border: 1px solid rgb(235, 75, 75);
      border-radius: 10rpx;
      text-align: center;
    }

    .pop-btn-confirm {
      width: 75%;
      height: 52rpx;
      font-size: 28rpx;
      border-radius: 10rpx;
      background-color: rgb(235, 75, 75);
      color: #FFFFFF;
      text-align: center;
    }
  }
}

</style>
